package com.szholly.pro.watergas_server.ps.monitor;


import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.util.RedisHelper;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import org.htmlparser.util.ParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/MonitorData/*")
public class MonitorDataController extends BaseController {
	
	@Autowired
	MonitorDataService monitorDataService;
	
	/**
	 * 获取最新一条实时数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getLastRealTimeData")
	public void getLastRealTimeData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getLastRealTimeData(params);
		responseData(params, result,"","");
	}
	
	/**
	 * 实时数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getRealTimeData")
	public void getRealTimeData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getRealTimeData(params);
		responseData(params, result,"","");
	}
	
	/**
	 * 分钟数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMinuteData")
	public void getMinuteData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getMinuteData(params);
		responseData(params, result,"分钟数据","分钟数据");
	}
	
	/**
	 * 小时数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getHourData")
	public void getHourData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getHourData(params);
		responseData(params, result,"小时数据","小时数据");
	}
	
	/**
	 * 周期数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCycleData")
	public void getCycleData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getCycleData(params);
		responseData(params, result,"周期数据","周期数据");
	}
	
	/**
	 * 天数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDayData")
	public void getDayData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getDayData(params);
		responseData(params, result,"天数据","天数据");
	}
	
	/**
	 * 周数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getWeekData")
	public void getWeekData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getMonthDataReport(params);
		responseData(params, result,"周报表数据","周报表数据");
	}
	
	/**
	 * 月数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMonthData")
	public void getMonthData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getMonthData(params);
		responseData(params, result,"月数据","月数据");
	}
	
	/**
	 * 季度数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getQuarterData")
	public void getQuarterData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getQuarterDataReport(params);
		responseData(params, result,"季报表数据","季报表数据");
	}
	/**
	 * 年度数据
	 * gislin
	 * 2017年5月3日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getYearData")
	public void getYearData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = RequestUnit.getHashMapParmS();
		MonitorDataModel result=monitorDataService.getQuarterDataReport(params);
		responseData(params, result,"年报表数据","年报表数据");
	}
	
	/**
	 * 通用输出数据方法，对查出来的监测数据做统一包装
	 * 假如有个性化的续期，再各个方法里面单独写
	 * @param params
	 * @param result
	 * @throws IOException 
	 * @throws ParserException 
	 */
	private void responseData(Map<String,String> params,MonitorDataModel result,String title,String downloadfilename) throws ParserException, IOException{
		/**
		 * 输出格式
		 */
		String format = params.get("_format");
		String mn = params.get("MN");
		String waterType = params.get("WATER_TYPE");
		
		if(format == null || format.equals("json")){
			JSONObject json = JSONObject.fromObject(result, JsonUtil.getConfig());
			outString(json.toString());
		}		
		else if(format.equals("excel")){
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String start = params.get("START");
			String end = params.get("END");
			Date startDate = null;
			Date endDate = null;
			try {
				startDate = sf.parse(start);
				endDate = sf.parse(end);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			Calendar startCal = Calendar.getInstance();
			startCal.setTime(startDate);
			
			Calendar endCal = Calendar.getInstance();
			endCal.setTime(endDate);
			
			String stationName = RedisHelper.getInstance().getStationNameByMN(mn);
			
			title = startCal.get(Calendar.YEAR) + "年" + (startCal.get(Calendar.MONTH)+1) + "月" + startCal.get(Calendar.DAY_OF_MONTH) + "日 ~"
					+ endCal.get(Calendar.YEAR) + "年" + (endCal.get(Calendar.MONTH)+1) + "月" + endCal.get(Calendar.DAY_OF_MONTH) + "日 "+ stationName + title;
			
			HashMap<String, Object> excelData = new HashMap<>();
			excelData.put("dataList", result);
			excelData.put("title", title);
			excelData.put("START", params.get("START"));
			excelData.put("END", params.get("END"));
			excelData.put("WATER_TYPE", waterType);
			
			if("1".equals(waterType)){
				/**
				 * 地表水
				 */
				exportExcelWidthFreemarkerTemp("/monitorData/tableTemp.html", null,excelData, title+".xls");
			}
			else if("0".equals(waterType)){
				/**
				 * 污水处理厂
				 */
				exportExcelWidthFreemarkerTemp("/monitorData/tableTemp.html", null,excelData, title+".xls");
			}
			else if("2".equals(waterType)){
				/**
				 * 工业废水
				 */
				exportExcelWidthFreemarkerTemp("/monitorData/tableTemp.html", "/monitorData/DataReportWithChart.xls",excelData, title+".xls");
			}
			else if("4".equals(waterType)){
				/**
				 *  工业废气
				 */
				exportExcelWidthFreemarkerTemp("/WasteGas/tableTemp.html", "/monitorData/DataReportWithChart.xls",excelData, title+".xls");
			}			
		}
	}
}
